// import * as types from '@/store/mutation-types'
import Vue from 'vue'
import Router from 'vue-router'
import sidebarMenu from '@/admin/store/modules/sidebar-menu'
import iView from 'iview'

Vue.use(Router)

// Menu should have 2 levels.
function generrateRoutesFromMenu(menu = [], routes = []) {
    for (let i = 0, l = menu.length; i < l; i++) {
        let item = menu[i]
        if (item.component) {
            routes.push({
                name: item.name,
                path: item.path,
                component: item.component,
                meta: item.meta || {}
            })
        }
        if (item.children) {
            generrateRoutesFromMenu(item.children, routes)
        }
    }
    return routes
}

let routerList = generrateRoutesFromMenu(sidebarMenu)

const router = new Router({
    mode: 'history',
    routes: routerList
})

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start()
    next()
})

router.afterEach(route => {
    iView.LoadingBar.finish()
})

export default router
